<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="dataForm" :rules="rules" :model="formBase" label-position="left" label-width="150px">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="formBase.username" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="realname">
          <el-input v-model="formBase.realname" placeholder="请输入姓名" ></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="formBase.password" placeholder="请输入密码" ></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="rePassword">
          <el-input v-model="formBase.rePassword" placeholder="请确认密码" ></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="formBase.phone" placeholder="请输入手机号" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { updateData, addUserData } from '@/api/store'
export default {
  name: 'add',
  props: ['dialogTitle', 'storeData'],
  data() {
    const validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入新密码'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error('密码长度为6 - 18个字符'))
      } else {
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formBase.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false,
      formBase: {
        username: '',
        realname: '',
        password: '',
        phone: '',
        storeId: '',
        type: '',
        isAdmin: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        rePassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹层显示
    dialogFormV(val) {
      this.dialogFormVisible = true
      if (val !== undefined) {
        this.formBase = val
      }
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },

    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
            await addUserData({...this.formBase, storeId: this.storeData.id, type: this.storeData.type}).then((res) => {
              if (res.data.message) {
                  this.$message.error(res.data.message)
                  this.$emit('newDataes', this.formBase)
              }
              this.dialogFormH()
            })
          }
      })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
